<?php

/*
 * 获取体检中心不同订单的分页结果
 * @param $status  子体检中心的状态
 * @param $view 渲染到的视图
 */

class CenterServices{
    const DEFAULT_SEARCH_RADIUS = 12000;
    //获取所有有体检中心的城市
    public function getCenterRegion()
    {
        try {
            $model = B2bSubCenter::model()->getCities();
        } catch (Exception $exc) {
            return JSONHelper::ERROR_SERVICE;
        }
        
        return $model;
    }
    //根据省或者市获取体检中心
    public function getCentersByCity($arr)
    {
        $province = null;
        $city = null;
        $brandid = null;
        if(isset($arr["province"])){
            $province = trim($arr["province"]);
        }
        if(isset($arr["city"])){
            $city = trim($arr["city"]);
        }
        if(isset($arr["brandid"])){
            $brandid = trim($arr["brandid"]);
        }
//        if($province === null && $city === null){
//            return JSONHelper::ERROR_ILLEGAL_REQUEST;
//        }
        $criteria = new CDbCriteria();
        if($province !== null){
            $criteria->addCondition("province = '$province'");
        }
        if($city !== null){
            $criteria->addCondition("city = '$city'");
        }
        if($brandid !== null){
            $criteria->addCondition("brandid = $brandid");
        }
        $ret = B2bSubCenter::model()->findAll($criteria);
        $items = array();
        foreach($ret as $it){
            if($it->brand === null){
                continue;
            }
            $item["name"] = $it->name;
            $item["brandid"] = $it->brand->id;
            $item["brandname"] = $it->brand->name;
            if(strlen(trim($it->brand->icon)) == 0){
                $icon = Yii::app()->getBaseUrl()."/images/brand/app_default.png";
            }else{
                $icon = Yii::app()->getBaseUrl()."/".$it->brand->icon;
            }
            $item["icon"] = $icon;
            $item["gps_lat"] = $it->gps_lat;
            $item["gps_lon"] = $it->gps_lon;
            $item["telephone"] = $it->telephone;
            $item["province"] = $it->province;
            $item["city"] = $it->city;
            $item["address"] = $it->address;
            array_push($items, $item);
        }
        return $items;
    }
    /*
     * 获取体检中心并按照省市排序
     */
    public function getCentersOrderByCity($arr)
    {
        $ret = $this->getCentersByCity($arr);
        if(is_int($ret)){
            return $ret;
        }
        $items = array();
        foreach($ret as $it)
        {
            if(!isset($items[$it["province"]])){
                $items[$it["province"]] = array();
            }
            if(!isset($items[$it["province"]][$it["city"]])){
                $items[$it["province"]][$it["city"]] = array();
            }
            if(!isset($items[$it["province"]][$it["city"]][$it["brandid"]])){
                $items[$it["province"]][$it["city"]][$it["brandid"]] = array();
            }
            $items[$it["province"]][$it["city"]][$it["brandid"]][] = $it;
        }
        return $items;
    }

    public function getBrands()
    {
        $ret = B2bBrand::model()->findAll();
        if($ret == null){
            return JSONHelper::ERROR_SERVICE;
        }
        $colorconfig = array(
            "公立"=>"#3ab7ff",
            "家"=>"#ff6a6a",
            "VIP"=>"#00d09d",
            "私立"=>"#ffa439",
            "default"=>"#5360eb"
        );
        try {
            $items = array();
            foreach ($ret as $it){
                $item["id"] = $it->id;
                $item["name"] = $it->name;
                if(strlen(trim($it->icon)) == 0){
                $icon = Yii::app()->getBaseUrl()."/images/brand/app_default.png";
                }else{
                    $icon = Yii::app()->getBaseUrl()."/".$it->icon;
                }
                $item["icon"] = $icon;
                $item["description"] = $it->description;
                $item["tags"] = array();
                $tags = explode(";", $it->tags);
                foreach($tags as $tag){
                    if($tag == null)
                        continue;
                    $t["color"] = isset($colorconfig[$tag])?$colorconfig[$tag]:$colorconfig["default"];
                    $t["name"] = $tag;
                    array_push($item["tags"], $t);
                }
                $t["color"] = isset($colorconfig["家"])?$colorconfig["家"]:$colorconfig["default"];
                $t["name"] = B2bSubCenter::model()->count("brandid=".$it->id)."家";
                array_push($item["tags"], $t);
                array_push($items, $item);
            }
        } catch (Exception $exc) {
            return JSONHelper::ERROR_SERVICE;
            
        }

        
        return $items;
    }
    public function getCenterListByLatAndLon($lat,$lon,$radius = CenterServices::DEFAULT_SEARCH_RADIUS)
    {
        $ret = B2bSubCenter::model()->centerSearch($lat,$lon, $radius);
        $items = array();
        foreach($ret as $it){
			$item["id"] = $it->id;
            $item["name"] = $it->name;
            $item["province"] = $it->province;
            $item["city"] = $it->city;
            $item["address"] = $it->address;
            $item["gps_lat"] = $it->gps_lat;
            $item["gps_lon"] = $it->gps_lon;
            $item["telephone"] = $it->telephone;
            array_push($items,$item);
        }
        return $items;
    }
	public function getCenterDetails($id)
	{
		$it = B2bSubCenter::model()->findByPk($id);
		if($it === null){
			return JSONHelper::ERROR_SERVICE;
		}
		$item["cid"] = $it->id;
        $item["name"] = $it->name;
        $item["province"] = $it->province;
        $item["city"] = $it->city;
        $item["address"] = $it->address;
        $item["gps_lat"] = $it->gps_lat;
        $item["gps_lon"] = $it->gps_lon;
        $item["telephone"] = $it->telephone;
		return $item;
	}
    
}   

?>
 